package com.wildfire.controller;


import com.wildfire.web.Result;
import com.wildfire.service.OrderService;
import com.wildfire.service.SeckillService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


/**
 * Controller模板
 * 
 * @author zwxu5
 */
@Api(tags = "基础功能")
@RestController
@Slf4j
public class SeckillController {
    @Autowired
    private SeckillService seckillService;
    @Autowired
    private OrderService orderService;

    @GetMapping("/doSeckill")
    public ResponseEntity<Result> doSeckill(long productId) {
        //成功返回订单信息，失败提示秒杀失败
        boolean isSucces = seckillService.doSeckill(productId);
        if(isSucces){
            //异步创建订单
            orderService.createOrder(productId);
        }
        return ResponseEntity.ok(Result.success(isSucces));
    }

    @GetMapping("/doSeckillCache")
    public ResponseEntity<Result> doSeckillCache(long productId) {
        //成功返回订单信息，失败提示秒杀失败
        boolean isSucces = seckillService.doSeckillByCache(productId);
        if(isSucces){
            //异步创建订单
            orderService.descStockAndCreateOrder(productId);
        }
        return ResponseEntity.ok(Result.success(isSucces));
    }

    @GetMapping("/doSeckillLock")
    public ResponseEntity<Result> doSeckillLock(long productId) {
        //成功返回订单信息，失败提示秒杀失败
        boolean isSucces = seckillService.doSeckillByLock(productId);
        if(isSucces){
            //异步创建订单
            orderService.descStockAndCreateOrder(productId);
        }
        return ResponseEntity.ok(Result.success(isSucces));
    }

    @GetMapping("/initProductCache")
    public ResponseEntity<Result> initProductCache() {
        seckillService.initProductCache();
        return ResponseEntity.ok(Result.success(null));
    }


}
